<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang === 'zh'?'420px':'450px'" @close='close' top="20vh">
    <div class="dialogContentBox">
      <el-form ref="form" :model="record" :label-width="lang === 'zh'?'110px':'140px'" :rules="rules" label-position="left" style="margin:0 20px;">
        <el-form-item :label="$t('联动组名称')" prop="name">
          <el-input v-emoji v-model="record.name" style="width:217px;" ref="name" :disabled='record.id?true:false' size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('状态')" prop="status" class="linkState">
          <el-switch :width="45" v-model="record.status" active-color="#009966" class="status switch" inactive-color="#CCCCCC" active-value="on" inactive-value="off" :active-text="$t('开')" :inactive-text="$t('关')"></el-switch>
        </el-form-item>
        <el-form-item :label="$t('物理接口')" prop="member">
          <el-select v-model="record.member" :placeholder="$t('请选择出接口')" ref="member" multiple clearable collapse-tags filterable @change="changeSelect" size='small'>
            <div class="selectBox">
              <el-checkbox v-model="checked" :indeterminate="isIndeterminate" class="all" @change='selectAll'>{{$t('全选')}}</el-checkbox>
              <el-link :underline="false" @click='selectRe' class="selectRe" style="line-height: 19px;padding-left:20px;">{{$t('反选')}}</el-link>
            </div>
            <el-option v-for="item in policyList" :disabled="item.enPortMode === 3" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">{{$t('确 定')}}</el-button>
      <el-button @click="close">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addInterLink, editInterLink } from '@/api/network/api/link'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'LinkDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateMember = (rule, value, callback) => {
      if (value && value.length > 0) {
        callback()
      } else {
        callback(new Error(this.$t('不能为空')))
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: null,
        name: '',
        member: [],
        status: 'on'
      },
      recordCopy: {
        id: null,
        name: '',
        member: [],
        status: 'on'
      },
      checked: false,
      isIndeterminate: false,
      policyList: [],
      memberListAll: [],
      lang: this.$i18n.locale,
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        member: [{ required: false, validator: validateMember, trigger: 'change' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      if (data) {
        this.record.id = data.id
        this.record.status = data.switch
        this.record.name = data.name
        this.record.member = []
        data.member.forEach(item => {
          this.record.member.push(item.interface)
        })
        this.changeSelect(this.record.member)
      }
      getPhysicalApiArray().then(res => {
        if (res.status === 0) {
          this.policyList = res.info
          this.memberListAll = []
          this.policyList.forEach(item => {
            if (item.enPortMode !== 3) {
              this.memberListAll.push(item.strPhysicsName)
            }
          })
        }
      })
      // 自动化
      this.$nextTick(() => {
        const data = ['name', 'member']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!this.record.id) {
            addInterLink(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            editInterLink(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    close () {
      this.checked = false
      this.isIndeterminate = false
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.policyList = []
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    selectAll () {
      this.record.member = []
      if (this.checked) {
        this.policyList.forEach((item) => {
          if (item.enPortMode !== 3) {
            this.record.member.push(item.strPhysicsName)
          }
        })
      } else {
        this.record.member = []
      }
      this.isIndeterminate = false
    },
    changeSelect (val) {
      let checkedCount = val.length
      this.checked = checkedCount === this.memberListAll.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.memberListAll.length
    },
    selectRe () {
      let check_array = []
      for (let item of this.record.member) {
        if (item.enPortMode !== 3) {
          check_array.push(item)
        }
      }
      this.record.member = this.memberListAll.filter((x) => !check_array.some((item) => x === item))
      this.changeSelect(this.record.member)
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(80vh - 184px);
  overflow-y: auto;
}
.selectBox {
  padding: 0 20px;
}
</style>
